/*
 * Client.cpp
 *
 *  Created on: Sep 8, 2012
 *      Author: dimir
 */

#include "Client.h"
#include <cstddef>

u_int32_t Client::all_id = 0;


Client::Client() {
	dbg_msg(LOG_TRACE, "Constructor of class Client");
	this->server = NULL;
	this->msg = NULL;
	this->id = ++all_id;
	this->registered = false;
}

Client::Client(Server* server) {
	dbg_msg(LOG_TRACE, "Constructor of class Client");
	this->server = server;
	this->msg = NULL;
	this->id = ++all_id;
	this->registered = false;
}

bool Client::hasNewMessage(){
	return(this->msg != NULL);
}

int Client::send(Message& msg){
	int ret =  server->recv(msg);
	msg.clear();
	return ret;
}
u_int32_t Client::getId(){
	return this->id;
}
Message* Client::readMessage(){
	Message* res = new Message(*this->msg);
	this->msg->clear();
	this->msg = NULL;
	delete this->msg;
	return res;
}

//0 - сообщение принято корректно
//не 0 - сообщение не было обработано
int Client::recv(Message& msg){
	if (this->msg != NULL){
		this->msg->clear();
		this->msg = NULL;
	}
	if (msg.getType() ==RESPONSE_REGISTRATION){
		this->registered = true;
	}
	if (msg.getType() ==RESPONSE_UNREGISTRATION){
		this->registered = false;
	}
	this->msg = new Message(msg);
	msg.clear();
	return 0;
}

void Client::registration(){
	while (!server->synchro()){
	}
	server->sleep();
	Message* message = genReqReg(this->id, this);
	if (this->send(*message) == 0){
		std::cout << "The request for registration"
				" of the client with id " << id
				<<" has been successfully"
				" delivered to the server" << std::endl;
		std::cout.flush();
	}
	delete message;
	if (this->hasNewMessage()){
		message = this->readMessage();
		if (message->getType() == RESPONSE_REGISTRATION){
			std::cout << "Client with id " << id
					<< " is registered in server" << std::endl;
			this->registered = true;
		}
		delete message;
	}else{
		dbg_msg(LOG_DEBUG, "bug in registration");
	}
	server->ready();
}

void Client::unregistration(){
	while (!server->synchro()){
	}
	server->sleep();
	Message* message = genExit(this->id);
	if (this->send(*message) == 0){
		std::cout << "The request for unregistration" <<
				" of the client with id " << id
				<<" has been successfully" <<
				" delivered to the server" << std::endl;
		std::cout.flush();
	}
	delete message;
	if (this->hasNewMessage()){
		message = this->readMessage();
		if (message->getType() == RESPONSE_UNREGISTRATION){
			std::cout << "Client with id " << id
					<< " is unregistered in server" << std::endl;
			this->registered = false;
		}
		delete message;
	}else{
		dbg_msg(LOG_DEBUG, "bug in unregistration");
	}
	server->ready();
}

bool Client::isRegistered(){
	return (this->registered);
}


void Client::exit(){
	while (!server->synchro()){
	}
	server->sleep();
	Message* message = genExit(this->id);
	if (this->send(*message) == 0){
		std::cout << "The request for exit from"
				" server the client with id " << id
				<<" has been successfully"
				" delivered to the server" << std::endl;
		std::cout.flush();
	}
	delete message;
	this->registered = false;
	server->ready();
}

Client::~Client() {
	dbg_msg(LOG_TRACE, "Desctructor of class Client");
	if (isRegistered()){
		if (this->server != NULL){
			// TODO !!!
			//послать сообщение с просьбой нас отключить от сервера
			Message* message = genExit(this->id);
			this->server->recv(*message);
			this->server = NULL;
		};
	}

}

